//
//  JHAssetGirdViewCell.m
//  JHAssetsPhotoPickerViewController
//
//  Created by admin on 15/11/17.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "JHAssetGirdViewCell.h"
#import "UIImage+JHAssetsPicker.h"

@interface JHAssetGirdViewCell ()

@end

@implementation JHAssetGirdViewCell

- (void)awakeFromNib {
    // Initialization code
    // Selection button
    [self setBackgroundColor:[UIColor grayColor]];
    _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectedButton.contentMode = UIViewContentModeTopRight;
    _selectedButton.adjustsImageWhenHighlighted = NO;
    [_selectedButton setImage:[UIImage imageForResourcePath:@"JHAssetsBrowser.bundle/ImageSelectedSmallOff" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateNormal];
    [_selectedButton setImage:[UIImage imageForResourcePath:@"JHAssetsBrowser.bundle/ImageSelectedSmallOn" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateSelected];
    [_selectedButton addTarget:self action:@selector(selectionButtonPressed) forControlEvents:UIControlEventTouchDown];
    _selectedButton.hidden = YES;
    _selectedButton.frame = CGRectMake(0, 0, 31, 31);
    [self addSubview:_selectedButton];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _selectedButton.frame = CGRectMake(self.bounds.size.width - _selectedButton.frame.size.width - 0,
                                       0, _selectedButton.frame.size.width, _selectedButton.frame.size.height);
}
- (void)displaySelectionButton
{
    if(!_selectedButton.hidden){
        if(self.delegate && [self.delegate respondsToSelector:@selector(photoSelectionWithIndex:)])
           [_selectedButton setSelected:[self.delegate photoSelectionWithIndex:self.index]];
    }
}
- (void)selectionButtonPressed
{
    if(_selectedButton.selected){
        [self setSelection:NO];
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(photoCanSelected:)]){
            if([self.delegate photoCanSelected:self.index]){
                [self setSelection:YES];
            }
        }
    }
}
- (void)setSelection:(BOOL)selection
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(photoWithIndex:changeSelection:)])
    {
        [self.delegate photoWithIndex:self.index changeSelection:selection];
        [self displaySelectionButton];
    }
}
@end
